package com.example.status;

import com.example.model.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * ClassName: ListStateDemo
 * Package: com.example.status
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-26
 * Time: 15:32
 */

//列表状态
public class ListStateDemo {
    //针对每种传感器输出最高的3个水位值
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        SingleOutputStreamOperator<WaterSensor> map = env.socketTextStream("hadoop102", 7777)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        final String[] s = value.split(" ");

                        return new WaterSensor(s[0], Long.valueOf(s[1]), Integer.valueOf(s[2]));
                    }
                });
        //没有分配水位线

        //keyBy
        map.keyBy(value -> value.getId())
                .process(new KeyedProcessFunction<String, WaterSensor, String>() {

                    ListState<Integer> lastVc;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        //获取的状态也是对应的 getState是Value  getListState是List
                        lastVc = getRuntimeContext().getListState(new ListStateDescriptor<Integer>("last", Types.INT));
                    }

                    @Override
                    public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                        //针对每种传感器输出最高的3个水位值

                        //1.获取数据之后 添加到状态当中
                        Integer vc = value.getVc();
                        lastVc.add(vc);

                        //2.排序取前三 但是不没有提供相应的排序方法
                        //遍历到 arrayList当中
                        ArrayList<Integer> list = new ArrayList<>();
                        for (Integer val : lastVc.get()) {
                            list.add(val);

                        }
                        //降序 后 减去 钱
                        list.sort(((o1, o2) -> o2 - o1));
                        //有空指针
                        if (list.size() > 3) {
                            list.remove(3);
                        }
                        out.collect("前三的水位值" + list);

                        //对状态更新
                        lastVc.update(list);

                    }
                }).print();
        env.execute();

    }
}
